package routers

import (
	"github.com/gin-gonic/gin"
	"gitee.com/laofa1/play/k_conf_mng/kconf_backend/middleware/jwt"
	"gitee.com/laofa1/play/k_conf_mng/kconf_backend/middleware/session"
	"gitee.com/laofa1/play/k_conf_mng/kconf_backend/pkg/setting"
	"gitee.com/laofa1/play/k_conf_mng/kconf_backend/routers/api"
	admin "gitee.com/laofa1/play/k_conf_mng/kconf_backend/routers/api/admin"
	v1 "gitee.com/laofa1/play/k_conf_mng/kconf_backend/routers/api/v1"
)

func InitRouter() *gin.Engine {
	r := gin.New()

	r.Use(gin.Logger())
	r.Use(gin.Recovery())

	gin.SetMode(setting.RunMode)

	r.POST("/api/auth", api.GetAuth)
	r.POST("/api/getconfig", api.GetConfig)

	apiv1 := r.Group("/api/v1")
	apiv1.Use(jwt.JWT())
	apiv1.Use(session.SessionAuth())
	{
		apiv1.GET("/currentUser", v1.GetCurrentUser)
		apiv1.GET("/users", v1.GetUsers)
		apiv1.POST("/user/password", v1.SetPassword)
		apiv1.GET("/notices", v1.GetNotices)
		apiv1.GET("/kconfig/items", v1.GetConfItems)
		apiv1.POST("/kconfig/items", v1.PostConfItems)
		apiv1.GET("/kconfig/template", v1.GetLastTemplate)
		apiv1.POST("/kconfig/template", v1.UpdateTemplate)
		apiv1.GET("/kconfig/instances", v1.GetInstances)
		apiv1.POST("/kconfig/instance/delete", v1.RemoveInstance)
		apiv1.POST("/kconfig/instance", v1.CloneInstance)
		apiv1.GET("/kconfig/gettokens", v1.GetConfTokens)
		apiv1.POST("/kconfig/gettoken/generate", v1.GenerateConfToken)
		apiv1.POST("/kconfig/gettoken/delete", v1.RemoveGetToken)
		apiv1.GET("/kconfig/getconfig", v1.GetConfig)
		apiv1.GET("/kconfig/templates", v1.GetAllTemplate)
		apiv1.GET("/kconfig/template/history", v1.GetTemplateById)

		apiv1.POST("/elasticenv/env/delete", v1.DeleteEnv)
		apiv1.POST("/elasticenv/env/create", v1.CreateEnv)
		apiv1.GET("/elasticenv/env/state", v1.GetEnvStates)
		apiv1.GET("/elasticenv/envs/state", v1.GetEnvsStates)
		apiv1.GET("/elasticenv/envs", v1.GetEnvs)
		apiv1.GET("/elasticenv/env", v1.GetEnv)
	}

	apiAdmin := r.Group("/api/admin")
	apiAdmin.Use(jwt.JWT())
	apiAdmin.Use(session.SessionAdminAuth())
	{
		apiAdmin.GET("/users", admin.GetUsers)
		apiAdmin.POST("/user/disable", admin.DisableUser)
		apiAdmin.POST("/user/enable", admin.EnableUser)
		apiAdmin.POST("/user/add", admin.CreateUser)
		apiAdmin.POST("/user/role", admin.SetRole)
		apiAdmin.POST("/user/password", admin.SetPassword)
	}

	return r
}
